package wang.ddcat.reptilian_plus.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil {

    /* 微信:
        user_agent:
            Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255
        referer:
            https://mp.weixin.qq.com
     */
    private static String user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
    private static String referer = "https://mp.weixin.qq.com";
    private static boolean use_caches = false;
    private static int timeout = 5000;
    private static String charset_name = "UTF-8";



    public static String sendGet(String url) throws Exception{
        StringBuffer result = new StringBuffer();
        URL realURL = new URL(url);
        URLConnection conn = realURL.openConnection();
        // 设置是否使用缓存
        conn.setUseCaches(use_caches);
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        //用户标识
        conn.setRequestProperty("user-agent", user_agent);
        //告诉服务器是从哪个页面链接过来的
        conn.setRequestProperty("RefererConstant", referer);
        //连接时间
        conn.setConnectTimeout(timeout);
        conn.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        String line;
        while ((line = in.readLine()) != null) {
            result.append(line);
            result.append("\n");
        }
        return result.toString();
    }
 
    public static String sendPost(String url,String param) throws Exception{
        String result = "";
        URL realUrl = new URL(url);
        URLConnection conn = realUrl.openConnection();
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent", user_agent);
        conn.setRequestProperty("RefererConstant", referer);
        //post设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        PrintWriter out = new PrintWriter(conn.getOutputStream());
        out.print(param);
        out.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
        String line;
        while((line = in.readLine()) != null){
            result += line;
        }
        return result;
    }


    public HttpUtil user_agent(String user_agent){
        this.user_agent = user_agent;
        return this;
    }
    public HttpUtil referer(String referer){
        this.referer = referer;
        return this;
    }
    public HttpUtil use_caches(boolean use_caches){
        this.use_caches = use_caches;
        return this;
    }
    public HttpUtil timeout(int timeout){
        this.timeout = timeout;
        return this;
    }

    public HttpUtil charset_name(String charset_name) {
        this.charset_name = charset_name;
        return this;
    }
}
